package TCPMain;

import android.util.Log;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import TCPMain.utils.DataUtil;

/**
 * 解析服务器发过来的json
 */
public class JsonAnalysis {
    String TAG="JsonAnalysis";
    JSONObject data;
    DataUtil dataUtil=new DataUtil();

    public JsonAnalysis(String data) throws JSONException {
        this.data = new JSONObject(data);
    }

    public JsonAnalysis(JSONObject data) {
        this.data = data;
    }

    public void setData(JSONObject data) {
        this.data = data;
    }

    /**
     * 获取JSON中的文件夹列表
     * @param data
     * @return
     * @throws JSONException
     */
    public List<String> GetFolders(JSONObject data) throws JSONException {
        List<String> out=new ArrayList<>();
        JSONArray temp = data.getJSONArray("folders");
        String one=temp.getString(0);//判断是否为空
        if(one.equals("")){
            Log.d(TAG,"文件夹为空");
            return out;
        }
        for (int i = 0; i < temp.length(); i++) {
            String Folder = temp.getString(i);
            out.add(Folder);
        }
        return out;
    }
    /**
     * 获取JSON中的文件夹列表
     * @param data
     * @return
     * @throws JSONException
     */
    public List<String> GetFolders(String data) throws JSONException {
        JSONObject JsonData=dataUtil.GetData(data);
        List<String> out=new ArrayList<>();
        JSONArray temp = JsonData.getJSONArray("folders");
        String one=temp.getString(0);//判断是否为空
        if(one.equals("")){
            Log.d(TAG,"文件夹为空");
            return out;
        }
        for (int i = 0; i < temp.length(); i++) {
            String Folder = temp.getString(i);
            out.add(Folder);
        }
        return out;
    }
}
